package com.yugao.fintech.antelope.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.antelope.auth.assembly.ExtLdpAssembly;
import com.yugao.fintech.antelope.auth.assembly.ExtLdpConnectAssembly;
import com.yugao.fintech.antelope.auth.manager.ExtLdpConnectManager;
import com.yugao.fintech.antelope.auth.mapper.ExtLdpMapper;
import com.yugao.fintech.antelope.auth.model.entity.ExtLdp;
import com.yugao.fintech.antelope.auth.model.entity.ExtLdpConnect;
import com.yugao.fintech.antelope.auth.model.rqrs.ldp.*;
import com.yugao.fintech.antelope.auth.service.ExtLdpService;
import com.yugao.fintech.antelope.base.exception.enums.GlobalErrorEnum;
import com.yugao.fintech.antelope.base.model.BasePlusPO;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import com.yugao.fintech.framework.mybatisplus.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 外部身份源模板
 */
@Slf4j
@Service
public class ExtLdpServiceImpl extends ServiceImpl<ExtLdpMapper, ExtLdp> implements IService<ExtLdp>, ExtLdpService {
    @Autowired
    private ExtLdpConnectManager connectionManager;
    @Autowired
    private ExtLdpConnectAssembly ldpConnectAssembly;
    @Autowired
    private ExtLdpAssembly extLdpAssembly;

    @Override
    public IPage<ExtLdpResp> pageExtLdp(ExtLdpPageReq req) {
        LambdaQueryWrapper<ExtLdp> lqw = ExtLdp.lqw()
                .eq(StringUtils.isNotEmpty(req.getType()), ExtLdp::getType, req.getType())
                .like(StringUtils.isNotEmpty(req.getName()), ExtLdp::getName, req.getName())
                .orderByDesc(ExtLdp::getCreatedAt);
        IPage<ExtLdp> pageData = this.page(PageUtil.to(req), lqw);
        return PageUtil.to(pageData, extLdpAssembly::toExtLdpResp);
    }

    @Override
    public List<ExtLdpResp> listExtLdp(ExtLdpListReq req) {
        LambdaQueryWrapper<ExtLdp> lqw = ExtLdp.lqw()
                .like(StringUtils.isNotEmpty(req.getName()), ExtLdp::getName, req.getName())
                .orderByDesc(BasePlusPO::getCreatedAt);
        return this.list(lqw).stream().map(extLdpAssembly::toExtLdpResp).collect(Collectors.toList());
    }

    @Override
    public ExtLdpResp getExtLdp(String code) {
        ExtLdp entity = this.getOne(ExtLdp.lqw().eq(ExtLdp::getCode, code));
        if (Objects.isNull(entity)) {
            throw new BizException(GlobalErrorEnum.OPERATION_FAIL_SELETE);
        }

        // 获取模板
        List<ExtLdpConnect> connections = connectionManager.listByLdpId(entity.getId());
        ExtLdpResp resp = extLdpAssembly.toExtLdpResp(entity);

        List<ExtLdpConnectResp> connectionRespList = connections.stream()
                .map(ldpConnectAssembly::toConnectResp)
                .collect(Collectors.toList());
        resp.setConnections(connectionRespList);
        return resp;
    }

    @Override
    public boolean saveExtLdp(ExtLdpSaveReq req) {
        long count = this.count(ExtLdp.lqw().eq(ExtLdp::getCode, req.getCode()));
        if (count > 0) {
            throw new BizException("身份源已存在");
        }
        ExtLdp entity = extLdpAssembly.toExtLdp(req);
        return this.save(entity);
    }

    @Override
    public boolean updateExtLdp(ExtLdpUpdateReq req) {
        long count = this.count(ExtLdp.lqw().eq(ExtLdp::getCode, req.getCode()).ne(ExtLdp::getId, req.getId()));
        if (count > 0) {
            throw new BizException("身份源已存在");
        }
        ExtLdp entity = extLdpAssembly.toExtLdp(req);
        return this.updateById(entity);
    }

    @Override
    public boolean deleteExtLdp(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean batchDeleteExtLdp(List<Long> ids) {
        return this.removeByIds(ids);
    }
}
